package h0913;

import java.util.*;

public class GroupAnagrams {
    public List<List<String>> groupAnagrams(String[] strs) {
        // 你的代码在这里实现
        HashMap<String, List<String>> map = new HashMap<>();
        int n = strs.length;
        for (int i = 0; i < n; i++) {
            String str = strs[i];
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String temp=new String(chars);
            if (map.containsKey(temp)){
                map.get(temp).add(str);
            }else {
                List<String> list = new ArrayList<>();
                list.add(str);
                map.put(temp,list);
            }
        }
        return map.values().stream().toList();
    }

    public static void main(String[] args) {
        GroupAnagrams ga = new GroupAnagrams();
        String[] input = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> result = ga.groupAnagrams(input);
        for (List<String> group : result) {
            System.out.println(group);
        }
    }
}
